// pages/index/index-revision1/index-revision1.js
var request = require('../../../utils/request.js');
var util = require('../../../utils/util.js');
// 引入SDK核心类
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
//获取应用实例
const app = getApp()
var timer;
var timer2;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: true,
    location:"",
    firstList:[
      {
        imgUrl:"../../../images/pic1.jpg",
        title:"小蝌蚪找妈妈",
        content:"好看好看好看"
      },
      {
        imgUrl: "../../../images/pic1.jpg",
        title: "小蝌蚪找妈妈",
        content: "好看好看好看特别好看"
      },
      {
        imgUrl: "../../../images/pic1.jpg",
        title: "小蝌蚪找妈妈",
        content: "好看好看好看好看好看好看特别好看"
      }
    ],
    firstList1: [
      {
        imgUrl: "../../../images/pic1.jpg",
        title: "小蝌蚪找妈妈",
        content: "好看好看好看"
      },
      {
        imgUrl: "../../../images/pic1.jpg",
        title: "小蝌蚪找妈妈",
        content: "好看好看好看特别好看"
      },
      {
        imgUrl: "../../../images/pic1.jpg",
        title: "小蝌蚪找妈妈",
        content: "好看好看好看好看好看好看特别好看"
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //调用当前位置
    that.getToken();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(getApp().globalData.location)
    this.setData({
      location: getApp().globalData.location
    })
  },
  getToken: function () {
    var that = this;
    // 获取本地存储中的token
    wx.getStorage({
      key: 'token',
      success: function (res) {//有该数据
        console.log(res.data)
        that.setData({
          token: res.data
        })
        that.loadAddress(res.data);
      },
      fail: function (res) {
        console.log(res.data)
        //调用授权方法-设置token
        app.getUserInfo();
      }
    })
  },
  loadAddress: function (param_token) {
    //页面展示选择地址
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        // 成功返回函数
        console.log(res)
        var longitude = res.longitude;
        var latitude = res.latitude;

        // 实例化API核心类
        var demo = new QQMapWX({
          key: 'UBGBZ-EWK3Q-C2Z5A-G3HJT-NOWSH-4BFIF' // 必填
        });
        // 调用接口
        demo.reverseGeocoder({
          location: {
            latitude: latitude,
            longitude: longitude
          },
          success: function (res) {
            console.log(res);
            that.setData({
              location: res.result.address,
              lat: res.result.ad_info.location.lat,
              lng: res.result.ad_info.location.lng,
            })
            getApp().globalData.location = res.result.address
            console.log(getApp().globalData.location)
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        });
      },
      fail: function (res) {
        console.log(res)
        wx.getSetting({
          success(res) {
            if (!res.authSetting['scope.userLocation']) {
              wx.showModal({
                title: '友情提示',
                content: '请允许获取地理信息，来获取附近书柜',
                showCancel: false,
                confirmText: "允许",
                confirmColor: "rgb(49,188,0)",
                success: function (res) {
                  if (res.confirm) {
                    wx.openSetting({
                      success: (res) => {
                        res.authSetting = {
                          "scope.userLocation": true
                        }
                        that.loadAddress(param_token);
                      },
                      fail: function (res) {
                        console.log(res)
                      }
                    })
                  } else if (res.cancel) {
                    setTimeout(function () {
                      wx.showToast({
                        title: '未允许获取信息',
                        image: "../../images/xx.png"
                      })
                    }, 800)
                  }
                }
              })
            }
          }
        })

      }
    })

  },
  gomine: function () {
    wx.redirectTo({
      url: "../../my/my-revision/my-revision"
    })
  },
  goMenu: function (e) {
    wx.navigateTo({
      url: '../index-nearby/index-nearby',
    })
  },
  godeta:function(){
    wx.navigateTo({
      url: '../../details/details',
    })
  },
  goage: function () {
    wx.navigateTo({
      url: '../index-age/index-age',
    })
  },
  gobook: function () {
    wx.navigateTo({
      url: '../index-Picture/index-Picture',
    })
  },
  changeName:function(e){
    console.log(e.currentTarget.id)
    var idNum = e.currentTarget.id
    if (idNum == "more1"){
      wx.navigateTo({
        url: "../index-more/index-more?id=1",
      })
    } else if (idNum == "more2"){
      wx.navigateTo({
        url: "../index-more/index-more?id=2",
      })
    }
   
  },
  goScanCode: function () {
    console.log(getCurrentPages())
    var that = this;

    wx.scanCode({
      success: (res) => {
        console.log(res)
        console.log(res.path.slice(21))
        that.setData({
          code: res.path.slice(21)
        })
        var tempCode = res.path.slice(21);

        if (tempCode && that.data.token) {
          var postData = {};
          postData.session_id = that.data.token;
          postData.qrcode = tempCode;
          postData.money = 99;
          console.log(postData)
          //扫码请求接口
          that.mainIndex(postData);
        }
      }
    })
  },
  mainIndex: function (param_data) {
    var that = this;
    request.addDeposit(
      param_data,
      (res) => {
        console.log(res.data);

        if (res.data.code == "10003" || res.data.code == "10004") {//未交押金
          var order_no = res.data.order_no;
          wx.showModal({
            title: '缴纳押金',
            content: '立即缴纳押金，开启智慧阅读生活',
            cancelText: "回首页",
            confirmText: "确定",
            confirmColor: "#FF0000",
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../my/my-wallet/my-addDeposit/my-addDeposit?order_no=' + order_no,
                })
              }
            }
          })
        } else if (res.data.code == "0") {//已交押金
          wx.navigateTo({
            url: '../openBookcase/openBookcase',
          })

        } else if (res.data.code == "10002") {//未支付的订单
          wx.showModal({
            title: '',
            content: res.data.msg,
            confirmColor: "#FF0000",
            confirmText: "去支付",
            cancelText: "返回",
            success: function () {

              wx.navigateTo({
                url: '../my/my-loanRecord/my-loanRecord?curId=2',
              })
            }
          })
        } else if (res.data.code == "10008") {//没有获取到手机号

          wx.showModal({
            title: '',
            content: "请到个人中心绑定手机号后再借阅图书哦~",
            confirmColor: "#FF0000",
            confirmText: "去绑定",
            cancelText: "取消",
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../my/my',
                })
              }
            }
          })


        } else {//10001请求失败,10005发送失败，10007他人正在使用中
          wx.showModal({
            title: '',
            content: res.data.msg,
            showCancel: false,
            confirmColor: "#FF0000"
          })
        }
      },
    )
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
 
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})